﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class NewDiaryEntry : System.Web.UI.Page
{
    private DateTime SelectedDate;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["SelectedDate"] == null)
                Response.Redirect("Default.aspx");
            else
            {
                SelectedDate = Convert.ToDateTime(Session["SelectedDate"]);
                lbSelectedDate.Text = SelectedDate.ToString("dd MMM yyyy");
                Calendar1.SelectedDate = SelectedDate;
            }
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Session["SelectedDate"] = Calendar1.SelectedDate;
        SelectedDate = Calendar1.SelectedDate;
        lbSelectedDate.Text = SelectedDate.ToString("dd MMM yyyy");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            using (var db = new DiaryOnlineDataContext())
            {
                SelectedDate = Convert.ToDateTime(Session["SelectedDate"]);
                DiaryEntry de = new DiaryEntry
                {
                    DiaryID = Convert.ToInt32(Session["DiaryID"]),
                    EntryDate = SelectedDate,
                    DateChanged = SelectedDate,
                    EntryTitle = txtTitle.Text,
                    EntryText = txtContent.Text
                };
                db.DiaryEntries.InsertOnSubmit(de);
                db.SubmitChanges();
                CustomValidator1.ErrorMessage = "Create Diary Entry Successfull";
                CustomValidator1.IsValid = false;
            }
        }
        catch (Exception ex)
        {
            CustomValidator1.ErrorMessage = ex.Message;
            CustomValidator1.IsValid = false;
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}
